﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusColOnOffBar : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { "Hidden" }; } }
        public override string[] TestStates { get { return new string[] { "Hidden", "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }
       
        public event EventHandler<Function> ColOnOff;
        public bool Hidden { get { return (bool)onOffToggle.IsChecked; } set { onOffToggle.IsChecked = value; ColOnOffsHide(value); } }
        public List<string> OnOffColumns { get { return _onOffColumns; } }

        private readonly List<string> _onOffColumns;
       
        //construct
        public FinPlusColOnOffBar(string caption, string onOffs)
        {
            InitializeComponent();

            onOffToggle.Content = caption;
            _onOffColumns = onOffs.Split(',').ToList<string>();
            foreach (var col in _onOffColumns)
                AddButton(col);

            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.ColOn:
                    case Commands.ColOff: ColOnOffs(func); break;
                    case Commands.ColOnOffsHide: ColOnOffsHide(bool.Parse(func["Tag"].ToString())); break;
                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose() { }
    
        //private
        private void AddButton(string name)
        {
            var button = new FinPlusColOnOffButton(name); 
            onOffBar.Children.Add(button);
            button.ColOnOff += ColOnOff_Click;
        }

        private void ColOnOffs(IFinPlusFunction func)
        {
            ColOnOff.Invoke(this, (Function)func);
        }

        private void ColOnOffsHide(bool hidden)
        {
            if (hidden)
            {
                onOffBar.Width = 0;
                onOffBar.IsEnabled = false;
            }
            else
            {
                onOffBar.Width = Double.NaN;
                onOffBar.IsEnabled = true;
            }
        }

        //cmds
        private new enum Commands { ColOn, ColOff, ColOnOffsHide }

        //events
        private void ColOnOff_Click(object sender, Function f){CommandRouter(f);}
        private void ColOnOffsHide_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColOnOffsHide.ToString(), Id, User, -1, ((ToggleButton)sender).IsChecked.ToString())); }
    }
}
